const { TreeNode, BinaryTreeTraversal } = require("../template")

function testBinaryTreeEdgeCases() {
  const traversal = new BinaryTreeTraversal()

  console.log(
    "1. 单节点树各种操作测试:",
    (() => {
      const singleNode = new TreeNode(42)
      return traversal.maxDepth(singleNode) === 1 &&
             traversal.isBalanced(singleNode) === true &&
             JSON.stringify(traversal.preorder(singleNode)) === JSON.stringify([42]) &&
             JSON.stringify(traversal.inorder(singleNode)) === JSON.stringify([42]) &&
             JSON.stringify(traversal.postorder(singleNode)) === JSON.stringify([42]) &&
             JSON.stringify(traversal.levelOrder(singleNode)) === JSON.stringify([[42]])
    })()
  )

  console.log(
    "2. 空树各种操作测试:",
    (() => {
      return traversal.maxDepth(null) === 0 &&
             traversal.isBalanced(null) === true &&
             JSON.stringify(traversal.preorder(null)) === JSON.stringify([]) &&
             JSON.stringify(traversal.inorder(null)) === JSON.stringify([]) &&
             JSON.stringify(traversal.postorder(null)) === JSON.stringify([]) &&
             JSON.stringify(traversal.levelOrder(null)) === JSON.stringify([])
    })()
  )

  console.log(
    "3. 两节点树测试:",
    (() => {
      const twoNodes = new TreeNode(1)
      twoNodes.left = new TreeNode(2)
      return traversal.maxDepth(twoNodes) === 2 &&
             traversal.isBalanced(twoNodes) === true &&
             JSON.stringify(traversal.preorder(twoNodes)) === JSON.stringify([1, 2]) &&
             JSON.stringify(traversal.inorder(twoNodes)) === JSON.stringify([2, 1]) &&
             JSON.stringify(traversal.postorder(twoNodes)) === JSON.stringify([2, 1]) &&
             JSON.stringify(traversal.levelOrder(twoNodes)) === JSON.stringify([[1], [2]])
    })()
  )

  console.log(
    "4. 特殊值节点测试:",
    (() => {
      const specialTree = new TreeNode(0)
      specialTree.left = new TreeNode(-1)
      specialTree.right = new TreeNode(1)
      return JSON.stringify(traversal.preorder(specialTree)) === JSON.stringify([0, -1, 1]) &&
             JSON.stringify(traversal.inorder(specialTree)) === JSON.stringify([-1, 0, 1]) &&
             JSON.stringify(traversal.postorder(specialTree)) === JSON.stringify([-1, 1, 0]) &&
             traversal.maxDepth(specialTree) === 2 &&
             traversal.isBalanced(specialTree) === true
    })()
  )

  console.log(
    "5. 字符串值节点测试:",
    (() => {
      const stringTree = new TreeNode("root")
      stringTree.left = new TreeNode("left")
      stringTree.right = new TreeNode("right")
      return JSON.stringify(traversal.preorder(stringTree)) === JSON.stringify(["root", "left", "right"]) &&
             JSON.stringify(traversal.inorder(stringTree)) === JSON.stringify(["left", "root", "right"]) &&
             JSON.stringify(traversal.postorder(stringTree)) === JSON.stringify(["left", "right", "root"]) &&
             traversal.maxDepth(stringTree) === 2 &&
             traversal.isBalanced(stringTree) === true
    })()
  )

  console.log(
    "6. this上下文边界测试:",
    (() => {
      const edgeObj = {
        multiplier: 2,
        traversal: new BinaryTreeTraversal(),
        processEdgeCase: function(root) {
          const depth = this.traversal.maxDepth(root)
          return depth * this.multiplier
        }
      }
      const singleNode = new TreeNode(1)
      return edgeObj.processEdgeCase(singleNode) === 2 // 1 * 2 = 2
    })()
  )

  console.log(
    "7. 复杂对象this绑定边界测试:",
    (() => {
      const edgeConfigObj = {
        config: { offset: 5 },
        traversal: new BinaryTreeTraversal(),
        processWithConfig: function(root) {
          const isBalanced = this.traversal.isBalanced(root)
          return isBalanced ? this.config.offset : 0
        }
      }
      const singleNode = new TreeNode(1)
      return edgeConfigObj.processWithConfig(singleNode) === 5 // 单节点树是平衡的
    })()
  )
}

testBinaryTreeEdgeCases()
